package com.china_goldcard.gridpagemenu;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import com.china_goldcard.pageindicatorview.PageIndicatorView;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页加载的菜单
 */
public class GridPageMenu extends RelativeLayout {

    public interface OnMenuItemClickListener{
        void onItemClicked(GridMenuModel item);
        void onItemLongClicked(GridMenuModel item);
    }

    private OnMenuItemClickListener menuItemClickListener;

    private List<GridMenuModel> dataList;

    private int columnNum = 4;

    private int rowNum = 2;

    private int menuBgColor;

    private int selectedColor;

    private int unselectedColor;

    private float titleSize = 12;

    private float itemH = 100;

    private float indicatorH = 20;

    private ViewPager viewPager;

    private PageIndicatorView indicatorView;

    public GridPageMenu(Context context) {
        this(context,null);
    }

    public GridPageMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        getConfig(context,attrs);
        initView(context);
    }

    public OnMenuItemClickListener getMenuItemClickListener() {
        return menuItemClickListener;
    }

    public void setMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {
        this.menuItemClickListener = menuItemClickListener;
    }

    /**
     * 从xml中获取配置信息
     */
    private void getConfig(Context context, AttributeSet attrs) {
        //TypedArray是一个数组容器用于存放属性值
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.GridPageMenu);
        columnNum = attributes.getInteger(R.styleable.GridPageMenu_gridPageColumnNum,4);
        rowNum = attributes.getInteger(R.styleable.GridPageMenu_gridPageMenuRowNum,2);
        titleSize = attributes.getDimensionPixelSize(R.styleable.GridPageMenu_gridPageMenuTitleSize,(int)sp2px(12));
        itemH = attributes.getDimension(R.styleable.GridPageMenu_gridPageMenuItemH,dp2px(30));
        menuBgColor = attributes.getColor(R.styleable.GridPageMenu_gridPageMenuBgColor,getResources().getColor(R.color.menu_bg_color));
        indicatorH = attributes.getDimension(R.styleable.GridPageMenu_gridPageMenuIndicatorH,dp2px(20));
        selectedColor = attributes.getResourceId(R.styleable.GridPageMenu_gridPageMenuSelectedColor,R.drawable.dot_selected);
        unselectedColor = attributes.getResourceId(R.styleable.GridPageMenu_gridPageMenuUnSelectedColor,R.drawable.dot_normal);
        //用完务必回收容器
        attributes.recycle();
    }

    /**
     * 初始化视图
     * @param context
     */
    private void initView(final Context context) {
        LayoutInflater.from(context).inflate(R.layout.grid_page_view, this, true);
        viewPager = (ViewPager) findViewById(R.id.page_menu_view_pager);
        indicatorView = (PageIndicatorView) findViewById(R.id.page_menu_indicator);
        indicatorView.setSelectedColor(selectedColor);
        indicatorView.setUnSelectedColor(unselectedColor);
        updateUI();
    }

    /**
     * 更新UI
     */
    private void updateUI(){
        viewPager.getLayoutParams().height = (int)(itemH * rowNum);
        viewPager.setBackgroundColor(menuBgColor);
        indicatorView.getLayoutParams().height = (int)indicatorH;
        indicatorView.setBackgroundColor(menuBgColor);
    }

    /**
     * 加载数据
     * @param dataList
     * @param fm
     */
    public <T extends GridMenuModel> void loadDataWithNoDataAdapter(List<T>dataList, FragmentManager fm){
        this.loadDataWithDataAdapter(dataList,fm,null);
    }

    /**
     * 加载数据
     * @param dataList
     * @param fm
     */
    public <T extends GridMenuModel> void loadDataWithDataAdapter(List<T>dataList, FragmentManager fm,GridMenuDataAdapter<T> dataAdapter){
        List<GridFragment> fragments = new ArrayList<>();
        int totalPage = 0;
        if (dataList.size() % (rowNum * columnNum) == 0){
            totalPage = dataList.size() / (rowNum * columnNum);
        }else{
            totalPage = dataList.size() / (rowNum * columnNum) + 1;
        }
        indicatorView.initData(totalPage);
        for(int index = 0 ; index < totalPage ; index ++){
            GridItemData<T> item = new GridItemData<>();
            item.setcPageNo(index);
            item.setDataList(dataList);
            item.setPageNo(rowNum);
            item.setItemH(itemH);
            item.setPageSize(columnNum);
            GridFragment gridFragment = GridFragment.newInstance(item);
            gridFragment.setDataAdapter(dataAdapter);
            gridFragment.setGridItemClickListener(new GridFragment.OnGridItemClickListener() {
                @Override
                public void onItemClicked(GridMenuModel item) {
                    if (menuItemClickListener!=null){
                        menuItemClickListener.onItemClicked(item);
                    }
                }

                @Override
                public void onItemLongClicked(GridMenuModel item) {
                    if (menuItemClickListener!=null){
                        menuItemClickListener.onItemLongClicked(item);
                    }
                }
            });
            fragments.add(gridFragment);
        }
        viewPager.setAdapter(new GridFragmentAdapter(fm,fragments));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicatorView.selectAtIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public float dp2px(float dp) {
        final float scale = getResources().getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }

    public float sp2px(float sp) {
        final float scale = getResources().getDisplayMetrics().scaledDensity;
        return sp * scale;
    }
}
